package jinyilw.swing.rlaf.ui;

import jinyilw.swing.ColorTools;
import jinyilw.swing.graphics.GraphicsTools;
import jinyilw.swing.graphics.RenderingConfig;
import jinyilw.swing.rlaf.theme.RThemeColor;

import javax.swing.JButton;
import javax.swing.JSplitPane;
import javax.swing.border.Border;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.plaf.basic.BasicSplitPaneUI;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.LayoutManager;

/**
 * @author Jinyi
 */
public class RSplitPaneDivider extends BasicSplitPaneDivider
{
	private static final long serialVersionUID = 9194142753450291864L;
	private static final Color bgColor = ColorTools.getAlphaColor(Color.white, 90);
	protected int oneTouchSize = 5;
	private boolean centerOneTouchButtons = false;

	public RSplitPaneDivider(BasicSplitPaneUI ui)
	{
		super(ui);
	}

	@Override
	public void paint(Graphics g)
	{
		int w = getWidth(), h = getHeight();
		g.setColor(bgColor);
		g.fillRoundRect(0, 0, w, h, 1, 1);
		g.setColor(RThemeColor.colorSet.textColor);
		Graphics2D g2d = (Graphics2D) g;
		RenderingConfig oldRenderingHints = RenderingConfig
				.enableAAPainting(g2d);
		GraphicsTools.paintGrip(g2d, 0, 0, w, h,
				orientation == JSplitPane.VERTICAL_SPLIT, 4, 3, 2, true);
		RenderingConfig.resetAAPainting(g2d, oldRenderingHints);
		super.paint(g);
	}

	@Override
	public void setDividerSize(int newSize)
	{
		dividerSize = newSize;
		// oneTouchSize = newSize / 2;
	}

	@Override
	protected JButton createLeftOneTouchButton()
	{
		JButton b = new JButton()
		{
			private static final long serialVersionUID = 7807222097337967740L;

			@Override
			public void setBorder(Border b)
			{
			}
			@Override
			public void paint(Graphics g)
			{
				if (splitPane != null)
				{
					int[] xs = new int[3];
					int[] ys = new int[3];
					int blockSize;
					g.setColor(RThemeColor.colorSet.textColor.brighter());
					if (orientation == JSplitPane.VERTICAL_SPLIT)
					{

						blockSize = Math.min(getHeight(), oneTouchSize);
						xs[0] = blockSize;
						xs[1] = 0;
						xs[2] = blockSize << 1;
						ys[0] = 0;
						ys[1] = ys[2] = blockSize;

					} else
					{
						blockSize = Math.min(getWidth(), oneTouchSize);
						xs[0] = xs[2] = blockSize;
						xs[1] = 0;
						ys[0] = 0;
						ys[1] = blockSize;
						ys[2] = blockSize << 1;
					}
					g.drawPolygon(xs, ys, 3);
					g.fillPolygon(xs, ys, 3);
				}
			}

			// Don't want the button to participate in focus traversable.
			@Override
			public boolean isFocusTraversable()
			{
				return false;
			}

			@Override
			public void updateUI()
			{
				setUI(new BasicButtonUI());
			}
		};
		b.setOpaque(false);
		b.setMinimumSize(new Dimension(oneTouchSize, oneTouchSize));
		b.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
		b.setFocusPainted(false);
		b.setBorderPainted(false);
		b.setRequestFocusEnabled(false);
		return b;
	}

	@Override
	protected JButton createRightOneTouchButton()
	{
		JButton b = new JButton()
		{
			private static final long serialVersionUID = 3971347426368154059L;

			@Override
			public void setBorder(Border border)
			{
			}

			@Override
			public void paint(Graphics g)
			{
				if (splitPane != null)
				{
					int[] xs = new int[3];
					int[] ys = new int[3];
					int blockSize;

					if (orientation == JSplitPane.VERTICAL_SPLIT)
					{
						blockSize = Math.min(getHeight(), oneTouchSize);
						xs[0] = blockSize;
						xs[1] = blockSize << 1;
						xs[2] = 0;
						ys[0] = blockSize;
						ys[1] = ys[2] = 0;
					} else
					{
						blockSize = Math.min(getWidth(), oneTouchSize);
						xs[0] = xs[2] = 0;
						xs[1] = blockSize;
						ys[0] = 0;
						ys[1] = blockSize;
						ys[2] = blockSize << 1;
					}
					g.setColor(RThemeColor.colorSet.textColor.brighter());
					g.drawPolygon(xs, ys, 3);
					g.fillPolygon(xs, ys, 3);
				}
			}

			@Override
			public boolean isFocusTraversable()
			{
				return false;
			}

			@Override
			public void updateUI()
			{
				setUI(new BasicButtonUI());
			}
		};
		b.setOpaque(false);
		b.setMinimumSize(new Dimension(oneTouchSize, oneTouchSize));
		b.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
		b.setFocusPainted(false);
		b.setBorderPainted(false);
		b.setRequestFocusEnabled(false);
		return b;
	}

	@Override
	public Dimension getMinimumSize()
	{
		return getPreferredSize();
	}

	class RDividerLayout implements LayoutManager
	{
		@Override
		public void layoutContainer(Container c)
		{
			if (leftButton != null && rightButton != null
					&& c == RSplitPaneDivider.this)
			{
				if (splitPane.isOneTouchExpandable())
				{
					Insets insets = getInsets();

					if (orientation == JSplitPane.VERTICAL_SPLIT)
					{
						int extraX = (insets != null) ? insets.left : 0;
						int blockSize = getHeight();

						if (insets != null)
						{
							blockSize -= (insets.top + insets.bottom);
							blockSize = Math.max(blockSize, 0);
						}
						blockSize = Math.min(blockSize, oneTouchSize);

						int y = (c.getSize().height - blockSize) / 2;

						if (!centerOneTouchButtons)
						{
							y = (insets != null) ? insets.top : 0;
							extraX = 0;
						}
						leftButton.setBounds(extraX + ONE_TOUCH_OFFSET, y,
								blockSize * 2, blockSize);
						rightButton.setBounds(
								extraX + ONE_TOUCH_OFFSET + oneTouchSize * 2, y,
								blockSize * 2, blockSize);
					} else
					{
						int extraY = (insets != null) ? insets.top : 0;
						int blockSize = getWidth();

						if (insets != null)
						{
							blockSize -= (insets.left + insets.right);
							blockSize = Math.max(blockSize, 0);
						}
						blockSize = Math.min(blockSize, oneTouchSize);

						int x = (c.getSize().width - blockSize) / 2;

						if (!centerOneTouchButtons)
						{
							x = (insets != null) ? insets.left : 0;
							extraY = 0;
						}

						leftButton.setBounds(x, extraY + ONE_TOUCH_OFFSET,
								blockSize, blockSize * 2);
						rightButton.setBounds(x,
								extraY + ONE_TOUCH_OFFSET + oneTouchSize * 2,
								blockSize, blockSize * 2);
					}
				} else
				{
					leftButton.setBounds(-5, -5, 1, 1);
					rightButton.setBounds(-5, -5, 1, 1);
				}
			}
		}

		@Override
		public Dimension minimumLayoutSize(Container c)
		{
			if (c != RSplitPaneDivider.this || splitPane == null)
			{
				return new Dimension(0, 0);
			}
			Dimension buttonMinSize = null;

			if (splitPane.isOneTouchExpandable() && leftButton != null)
			{
				buttonMinSize = leftButton.getMinimumSize();
			}

			Insets insets = getInsets();
			int width = getDividerSize();
			int height = width;

			if (orientation == JSplitPane.VERTICAL_SPLIT)
			{
				if (buttonMinSize != null)
				{
					int size = buttonMinSize.height;
					if (insets != null)
					{
						size += insets.top + insets.bottom;
					}
					height = Math.max(height, size);
				}
				width = 1;
			} else
			{
				if (buttonMinSize != null)
				{
					int size = buttonMinSize.width;
					if (insets != null)
					{
						size += insets.left + insets.right;
					}
					width = Math.max(width, size);
				}
				height = 1;
			}
			return new Dimension(width, height);
		}

		@Override
		public Dimension preferredLayoutSize(Container c)
		{
			return minimumLayoutSize(c);
		}

		@Override
		public void removeLayoutComponent(Component c)
		{
		}

		@Override
		public void addLayoutComponent(String string, Component c)
		{
		}
	}
}
